package com.lyl.syt.controller;/**
 * @author Mr.Luo
 * @date 2022-04-29 14:41
 */

import com.lyl.syt.common.result.Result;
import com.lyl.syt.model.cmn.Dict;
import com.lyl.syt.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Mr.Luo
 * @date 2022-04-29 14:41
 * @desc 数据字典控制器
 */
@Api(tags = "数据字典")
@RestController
@RequestMapping("/admin/cmn/dict")
@RequiredArgsConstructor
public class DictController {
    private final DictService dictService;

    @ApiOperation("根据parentId查询数据")
    @GetMapping("/findByParentId/{parentId}")
    public Result<List<Dict>> findByParentId(@PathVariable Long parentId){
        List<Dict> dictList = dictService.findByParentId(parentId);
        return Result.ok(dictList);
    }

    //根据dictcode获取下级节点
    @ApiOperation("根据dictcode获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(@PathVariable String dictCode){
        List<Dict> dictList = dictService.findByDictCode(dictCode);
        return Result.ok(dictList);
    }


    //根据dictcode和value查询
    @ApiOperation("根据dictcode和value查询")
    @GetMapping("/getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,@PathVariable String value){
        return dictService.getDictName(dictCode,value);
    }

    //根据value查询
    @ApiOperation("根据value查询")
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable String value){
        return dictService.getDictName("",value);
    }



    /**
     * 根据id获取子节点数据
     * @param id id
     * @return Result
     */
    @ApiOperation("根据id获取子节点数据")
    @GetMapping("/findChildData/{id}")
    public Result<List<Dict>> findChildData(@PathVariable Long id) {
        List<Dict> dictList = dictService.findChildData(id);
        return Result.ok(dictList);
    }

    /**
     * 导出excel
     * @param response
     * @return
     */
    @ApiOperation("导出excel")
    @GetMapping( value = "/exportExcel" ,produces = "application/octet-stream")
    public void exportExcel(HttpServletResponse response){
        dictService.exportExcel(response);
    }

    /**
     * 导入excel
     * @param file
     * @return
     */
    @ApiOperation("导入excel")
    @PostMapping("/importExcel")
    public Result<Boolean> importExcel(MultipartFile file){
        dictService.importExcel(file);
        return Result.ok();
    }

}
